function downloadObject(obj, down_type) {
    let $obj = $(obj)
    let status = $obj.parent('li').data('status')
    let _id = $obj.parent('li').data('id')
    let _name = $obj.parent('li').data('name')
    let _type_name = $obj.parents('ul').find('.type_name').text()
    if (status != '1') {
        layerFailed('任务转换未成功，不能下载')
        return
    }
    fetch('/ajax/downloadObject/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            'id': _id, 'down_type': down_type, 'name': _name
        })
    }).then(e => e.json()).then((ret) => {
        let code = ret.code || 400
        let data = ret.data || {}
        if (code != '200') {
            layerFailed(ret.msg)
            if (code == '413') {
                if (!checkVip()) return false
            }
            return
        }
        if (data.count > 1) {
            let donw_title = down_type == "vip_down" ? "下载" : "试用版下载"
            let _li = ''
            $.each(data.list.url, function (index, value) {
                _li += '<div class="layui-card-body layui-row"><li class="layui-input-inline layui-col-md8" style="width: 60%">' + value.file_name + '</li>' +
                    '<li class="layui-input-inline layui-col-md2" data-status="1" data-id="' + value.id + '" data-name="' + value.name + '">' +
                    '<button type="button" class="layui-btn layui-btn-sm layui-btn-normal" onclick="downloadObject(this,&quot;' + down_type + '&quot;)">' + donw_title + '</button></li></div>'
            })
            let _html = '<div class="layui-form">' + _li + '</div>'
            layer.open({
                title: '下载列表 - ' + _type_name,
                type: 1,
                skin: 'layui-layer-user',
                anim: 0,
                shade: [0.3, '#000'],
                area: ['42%', '50%'],
                shadeClose: true,
                zIndex: 50,
                closeBtn: 2,
                scrollbar: false,
                content: _html,
            })
        } else {
            let _index = layer.msg('下载中...', {
                shade: [0.3, '#000'],
                shadeClose: false,
                time: 1200
            })
            let _down_name = typeof _name == "undefined" ? data.list.file_name : _name
            download(data.list.url, _down_name, function (t) {
                layer.close(_index)
            })
        }
    })
}

function deleteObject(obj) {
    let $obj = $(obj)
    let _id = $obj.parent('li').data('id')
    layer.confirm('确定要删除吗？', {
        btn: ['确定', '取消'], 'title': '提示'
    }, function () {
        fetch('/ajax/deleteObject/', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                'id': _id
            })
        }).then(e => e.json()).then((ret) => {
            let code = ret.code || 400
            if (code != '200') {
                layerFailed(ret.msg)
                return
            }
            layerSuccess('删除成功')
            setTimeout(function () {
                location.reload()
            }, 800)
        })
    })
}

$(function () {
    let ws = new WebSocket(ws_url);
    if (_ids.length < 1) return
    let message = {
        "type": "get_task_status",
        "ids": _ids,
        "u_token": u_token
    };
    //连接成功建立后响应
    var _i = 0
    ws.onopen = function () {
        ws.send(JSON.stringify(message));
        setInterval(function () {
            if (_i >= 15) {
                location.reload()
                return
            }
            ws.send(JSON.stringify(message));//启动心跳
            _i++
        }, 2000)
    }
    //收到服务器消息后响应
    ws.onmessage = function (e) {
        let data = e.data
        data = JSON.parse(data)
        let code = data.code || 400
        if (code != '200') return
        let _list = data.data.list
        if (_list.length < 1) return;
        _list.forEach(function (eve) {
            if ($("." + eve.id).hasClass('success')) return false
            let _down_html = ''
            let _down_title = '下载'
            _down_html += '<button type="button" class="layui-btn layui-btn-sm layui-btn-normal" onclick="downloadObject(this,&quot;vip_down&quot)">' + _down_title + '</button>'
            $('.' + eve.id).addClass('success')
            $('.' + eve.id).find('.file_name').text(eve.file_name)
            $('.' + eve.id).find('.status').text('转换成功')
            $('.' + eve.id).find('.size').text(changeByte(eve.size))
            $('.' + eve.id).find('.download').attr('data-status', eve.status)
            $('.' + eve.id).find('.download').attr('data-id', eve.id)
            $('.' + eve.id).find('.download').prepend(_down_html)
        });
    }
})

$('*[lay-tips]').on('mouseenter', function () {
    let content = $(this).attr('lay-tips');
    this.index = layer.tips(content, this, {
        time: -1, tips: [1, '#3A3D49']
    });
}).on('mouseleave', function () {
    layer.close(this.index);
});